---
title: Spec - howl.util.table utilities
tags: spec
---
<div class="spec-group spec-group-1">

<h1 id="howl.util.table-utilities">howl.util.table utilities</h1>
<div class="spec-group spec-group-2">

<h2 id="delegate_to(base,-overlay_t)">delegate_to(base, overlay_t)</h2>

<h4 id="overlays-the-&lt;overlay_t&gt;-table-over-the-&lt;base&gt;-table">overlays the &lt;overlay_t&gt; table over the &lt;base&gt; table</h4>

<pre class="highlight moonscript"><code><span class="n">base</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ss">foo:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="ss">bar:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="ss">sub:</span><span class="w"> </span><span class="s1">'b'</span><span class="w"> </span><span class="p">}</span><span class="w">
</span><span class="n">over</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ss">zed:</span><span class="w"> </span><span class="mi">3</span><span class="w">
</span><span class="n">o</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">delegate_to</span><span class="w"> </span><span class="n">base</span><span class="p">,</span><span class="w"> </span><span class="n">over</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">o</span><span class="p">.</span><span class="n">foo</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="n">o</span><span class="p">.</span><span class="n">zed</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'b'</span><span class="p">,</span><span class="w"> </span><span class="n">o</span><span class="p">.</span><span class="n">bar</span><span class="p">.</span><span class="n">sub</span></code></pre>


<h4 id="the-overlay-is-applied-recursively-and-partially">the overlay is applied recursively and partially</h4>

<pre class="highlight moonscript"><code><span class="n">base</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ss">sub:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="ss">a:</span><span class="w"> </span><span class="s1">'a'</span><span class="p">,</span><span class="w"> </span><span class="ss">b:</span><span class="w"> </span><span class="s1">'b'</span><span class="p">,</span><span class="w"> </span><span class="ss">deep:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="ss">x:</span><span class="w"> </span><span class="s1">'x'</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="p">}</span><span class="w">
</span><span class="n">over</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ss">sub:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="ss">a:</span><span class="w"> </span><span class="s1">'A'</span><span class="p">,</span><span class="w"> </span><span class="ss">c:</span><span class="w"> </span><span class="s1">'c'</span><span class="p">,</span><span class="w"> </span><span class="ss">deep:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="ss">y:</span><span class="w"> </span><span class="s1">'Y'</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="p">}</span><span class="w">
</span><span class="n">o</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">delegate_to</span><span class="w"> </span><span class="n">base</span><span class="p">,</span><span class="w"> </span><span class="n">over</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'A'</span><span class="p">,</span><span class="w"> </span><span class="n">o</span><span class="p">.</span><span class="n">sub</span><span class="p">.</span><span class="n">a</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'b'</span><span class="p">,</span><span class="w"> </span><span class="n">o</span><span class="p">.</span><span class="n">sub</span><span class="p">.</span><span class="n">b</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'c'</span><span class="p">,</span><span class="w"> </span><span class="n">o</span><span class="p">.</span><span class="n">sub</span><span class="p">.</span><span class="n">c</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'Y'</span><span class="p">,</span><span class="w"> </span><span class="n">o</span><span class="p">.</span><span class="n">sub</span><span class="p">.</span><span class="n">deep</span><span class="p">.</span><span class="n">y</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'x'</span><span class="p">,</span><span class="w"> </span><span class="n">o</span><span class="p">.</span><span class="n">sub</span><span class="p">.</span><span class="n">deep</span><span class="p">.</span><span class="n">x</span></code></pre>


<h4 id="does-not-change-the-metatable-of-either-argument">does not change the metatable of either argument</h4>

<pre class="highlight moonscript"><code><span class="n">base_mt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ss">__index:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="ss">foo:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="ss">bar:</span><span class="w"> </span><span class="s1">'x'</span><span class="w"> </span><span class="p">}</span><span class="w">
</span><span class="n">base</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">setmetatable</span><span class="w"> </span><span class="p">{},</span><span class="w"> </span><span class="n">base_mt</span><span class="w">

</span><span class="n">over_mt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ss">__index:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="ss">foo:</span><span class="w"> </span><span class="s1">'foo'</span><span class="p">,</span><span class="w"> </span><span class="ss">zed:</span><span class="w"> </span><span class="s1">'y'</span><span class="w"> </span><span class="p">}</span><span class="w">
</span><span class="n">over</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">setmetatable</span><span class="w"> </span><span class="p">{},</span><span class="w"> </span><span class="n">over_mt</span><span class="w">

</span><span class="n">o</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">delegate_to</span><span class="w"> </span><span class="n">base</span><span class="p">,</span><span class="w"> </span><span class="n">over</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'foo'</span><span class="p">,</span><span class="w"> </span><span class="n">o</span><span class="p">.</span><span class="n">foo</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'x'</span><span class="p">,</span><span class="w"> </span><span class="n">o</span><span class="p">.</span><span class="n">bar</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'y'</span><span class="p">,</span><span class="w"> </span><span class="n">o</span><span class="p">.</span><span class="n">zed</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">base_mt</span><span class="p">,</span><span class="w"> </span><span class="nb">getmetatable</span><span class="p">(</span><span class="n">base</span><span class="p">)</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">over_mt</span><span class="p">,</span><span class="w"> </span><span class="nb">getmetatable</span><span class="p">(</span><span class="n">over</span><span class="p">)</span></code></pre>


<h4 id="provides-a-joint-view-of-the-keys-for-pairs()">provides a joint view of the keys for pairs()</h4>

<pre class="highlight moonscript"><code><span class="n">base</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ss">foo:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="ss">bar:</span><span class="w"> </span><span class="s1">'b'</span><span class="w">
</span><span class="n">over</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ss">zed:</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="ss">bar:</span><span class="w"> </span><span class="s1">'B'</span><span class="w">
</span><span class="n">o</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">delegate_to</span><span class="w"> </span><span class="n">base</span><span class="p">,</span><span class="w"> </span><span class="n">over</span><span class="w">
</span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="ss">foo:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="ss">bar:</span><span class="w"> </span><span class="s1">'B'</span><span class="p">,</span><span class="w"> </span><span class="ss">zed:</span><span class="w"> </span><span class="mi">3</span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="n">k</span><span class="p">,</span><span class="n">v</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">k</span><span class="p">,</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="nb">pairs</span><span class="p">(</span><span class="n">o</span><span class="p">)}</span></code></pre>

</div>
</div>
